ALTER TABLE NhaCungCap WITH NOCHECK ADD
CONSTRAINT [PK_Mancc] PRIMARY KEY CLUSTERED
(
	MaNCC
) ON [PRIMARY]
GO

ALTER FUNCTION fnGenMaNCC()
RETURNS char(8)
AS
BEGIN
	DECLARE @MaNCC char(8)
	IF (SELECT COUNT(MaNCC) FROM NhaCungCap)=0
		SET @MaNCC=0
	ELSE
		SELECT @MaNCC=Max(right(MaNCC,5)) FROM NhaCungCap
		SELECT @MaNCC=
	CASE
	WHEN @MaNCC>=0 and @MaNCC<9 THEN
		'NCC0000'+Convert (char, @MaNCC+1)
	WHEN @MaNCC>=9 and @MaNCC<99 THEN
		'NCC000'+Convert (char, @MaNCC+1)
	WHEN @MaNCC>=99 and @MaNCC<999 THEN
		'NCC00'+Convert (char, @MaNCC+1)
	WHEN @MaNCC>=999 and @MaNCC<9999 THEN
		'NCC0'+Convert (char, @MaNCC+1)
	WHEN @MaNCC>=9999 and @MaNCC<99999 THEN
		'NCC'+Convert (char, @MaNCC+1)
	WHEN @MaNCC>=99999 THEN
		Convert (char, @MaNCC+1)
END
RETURN @MaNCC
END

ALTER TABLE NhaCungCap 
ADD CONSTRAINT defMaNCC DEFAULT dbo.fnGenMaNCC()
FOR MaNCC

 
-----------------

  ALTER TABLE Hoa WITH NOCHECK ADD
CONSTRAINT [PK_Mahoa] PRIMARY KEY CLUSTERED
(
	Mahoa
) ON [PRIMARY]
GO

create FUNCTION fnGenMahoa()
RETURNS char(6)
AS
BEGIN
	DECLARE @Mahoa char(6)
	IF (SELECT COUNT(Mahoa) FROM Hoa)=0
		SET @Mahoa=0
	ELSE
		SELECT @Mahoa=Max(right(Mahoa,5)) FROM hoa
		SELECT @Mahoa=
	CASE
	WHEN @Mahoa>=0 and @Mahoa<9 THEN
		'H0000'+Convert (char, @Mahoa+1)
	WHEN @Mahoa>=9 and @Mahoa<99 THEN
		'H000'+Convert (char, @Mahoa+1)
	WHEN @Mahoa>=99 and @Mahoa<999 THEN
		'H00'+Convert (char, @Mahoa+1)
	WHEN @Mahoa>=999 and @Mahoa<9999 THEN
		'H0'+Convert (char, @Mahoa+1)
	WHEN @Mahoa>=9999 and @Mahoa<99999 THEN
		'H'+Convert (char, @Mahoa+1)
	WHEN @Mahoa>=99999 THEN
		Convert (char, @Mahoa+1)
END
RETURN @Mahoa
END

ALTER TABLE Hoa 
ADD CONSTRAINT defMaHoa DEFAULT dbo.fnGenMahoa()
FOR Mahoa
---------------------------------------------

  ALTER TABLE LoaiHoa WITH NOCHECK ADD
CONSTRAINT [PK_MaLoai] PRIMARY KEY CLUSTERED
(
	MaLoai
) ON [PRIMARY]
GO

alter FUNCTION fnGenMaLoai()
RETURNS char(6)
AS
BEGIN
	DECLARE @MaLoai char(6)
	IF (SELECT COUNT(MaLoai) FROM LoaiHoa)=0
		SET @MaLoai=0
	ELSE
		SELECT @MaLoai=Max(right(MaLoai,5)) FROM LoaiHoa
		SELECT @MaLoai=
	CASE
	WHEN @MaLoai>=0 and @MaLoai<9 THEN
		'L0000'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=9 and @MaLoai<99 THEN
		'L000'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=99 and @MaLoai<999 THEN
		'L00'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=999 and @MaLoai<9999 THEN
		'L0'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=9999 and @MaLoai<99999 THEN
		'L'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=99999 THEN
		Convert (char, @MaLoai+1)
END
RETURN @MaLoai
END

ALTER TABLE LoaiHoa 
ADD CONSTRAINT defMaLoai DEFAULT dbo.fnGenMaLoai()
FOR MaLoai
-------------------------------------------------------------
  ALTER TABLE HDNhap WITH NOCHECK ADD
CONSTRAINT [PK_SOHDN] PRIMARY KEY CLUSTERED
(
	SoHDN
) ON [PRIMARY]
GO

create FUNCTION fnGenSoHDN()
RETURNS char(6)
AS
BEGIN
	DECLARE @SoHDN char(6)
	IF (SELECT COUNT(SoHDN) FROM HDNHap)=0
		SET @SoHDN=0
	ELSE
		SELECT @SoHDN=Max(right(SoHDN,5)) FROM HDNhap
		SELECT @SoHDN=
	CASE
	WHEN @SoHDN>=0 and @SoHDN<9 THEN
		'N0000'+Convert (char, @SoHDN+1)
	WHEN @SoHDN>=9 and @SoHDN<99 THEN
		'N000'+Convert (char, @SoHDN+1)
	WHEN @SoHDN>=99 and @SoHDN<999 THEN
		'N00'+Convert (char, @SoHDN+1)
	WHEN @SoHDN>=999 and @SoHDN<9999 THEN
		'N0'+Convert (char, @SoHDN+1)
	WHEN @SoHDN>=9999 and @SoHDN<99999 THEN
		'N'+Convert (char, @SoHDN+1)
	WHEN @SoHDN>=99999 THEN
		Convert (char, @SoHDN+1)
END
RETURN @SoHDN
END

ALTER TABLE HDNhap 
ADD CONSTRAINT defSoHDN DEFAULT dbo.fnGenSoHDN()
FOR SoHDN
-------------------------------
  ALTER TABLE HDXuat WITH NOCHECK ADD
CONSTRAINT [PK_SOHDX] PRIMARY KEY CLUSTERED
(
	SoHDX
) ON [PRIMARY]
GO

create FUNCTION fnGenSoHDX()
RETURNS char(6)
AS
BEGIN
	DECLARE @SoHDX char(6)
	IF (SELECT COUNT(SoHDX) FROM HDXuat)=0
		SET @SoHDX=0
	ELSE
		SELECT @SoHDX=Max(right(SoHDX,5)) FROM HDXuat
		SELECT @SoHDX=
	CASE
	WHEN @SoHDX>=0 and @SoHDX<9 THEN
		'X0000'+Convert (char, @SoHDX+1)
	WHEN @SoHDX>=9 and @SoHDX<99 THEN
		'X000'+Convert (char, @SoHDX+1)
	WHEN @SoHDX>=99 and @SoHDX<999 THEN
		'X00'+Convert (char, @SoHDX+1)
	WHEN @SoHDX>=999 and @SoHDX<9999 THEN
		'X0'+Convert (char, @SoHDX+1)
	WHEN @SoHDX>=9999 and @SoHDX<99999 THEN
		'X'+Convert (char, @SoHDX+1)
	WHEN @SoHDX>=99999 THEN
		Convert (char, @SoHDX+1)
END
RETURN @SoHDX
END

ALTER TABLE HDXuat
ADD CONSTRAINT defSoHDX DEFAULT dbo.fnGenSoHDX()
FOR SoHDX
-------------------------------
  ALTER TABLE NhomChucNang WITH NOCHECK ADD
CONSTRAINT [PK_MaCn] PRIMARY KEY CLUSTERED
(
	MaCn
) ON [PRIMARY]
GO

alter FUNCTION fnGenMaCn()
RETURNS char(4)
AS
BEGIN
	DECLARE @MaCn char(4)
	IF (SELECT COUNT(MaCn) FROM NhomChucNang)=0
		SET @MaCn=0
	ELSE
		SELECT @MaCn=Max(right(MaCn,2)) FROM NhomChucNang
		SELECT @MaCn=
	CASE
	WHEN @MaCn>=0 and @MaCn<9 THEN
		'CN0'+Convert (char, @MaCn+1)
	WHEN @MaCn>=9 and @MaCn<99 THEN
		'CN'+Convert (char, @MaCn+1)
END
RETURN @MaCn
END

ALTER TABLE NhomChucNang
ADD CONSTRAINT defMaCn DEFAULT dbo.fnGenMaCn()
FOR MaCn
--------------------------
 ALTER TABLE TinTuc WITH NOCHECK ADD
CONSTRAINT [PK_MaTT] PRIMARY KEY CLUSTERED
(
	MaTT
) ON [PRIMARY]
GO

create FUNCTION fnGenMaTT()
RETURNS char(7)
AS
BEGIN
	DECLARE @MaTT char(7)
	IF (SELECT COUNT(MaTT) FROM TinTuc)=0
		SET @MaTT=0
	ELSE
		SELECT @MaTT=Max(right(MaTT,5)) FROM TinTuc
		SELECT @MaTT=
	CASE
	WHEN @MaTT>=0 and @MaTT<9 THEN
		'TT0000'+Convert (char, @MaTT+1)
	WHEN @MaTT>=9 and @MaTT<99 THEN
		'TT000'+Convert (char, @MaTT+1)
	WHEN @MaTT>=99 and @MaTT<999 THEN
		'TT00'+Convert (char, @MaTT+1)
	WHEN @MaTT>=999 and @MaTT<9999 THEN
		'TT0'+Convert (char, @MaTT+1)
	WHEN @MaTT>=9999 and @MaTT<99999 THEN
		'TT'+Convert (char, @MaTT+1)
	WHEN @MaTT>=99999 THEN
		Convert (char, @MaTT+1)
END
RETURN @MaTT
END

ALTER TABLE TinTuc
ADD CONSTRAINT defMaTT DEFAULT dbo.fnGenMaTT()
FOR MaTT
-------------------
--------------------------
 ALTER TABLE LoaiTT WITH NOCHECK ADD
CONSTRAINT [PK_MaLoai] PRIMARY KEY CLUSTERED
(
	MaLoai
) ON [PRIMARY]
GO

alter FUNCTION fnGenMaLoaiTT()
RETURNS char(8)
AS
BEGIN
	DECLARE @MaLoai char(8)
	IF (SELECT COUNT(MaLoai) FROM LoaiTT)=0
		SET @MaLoai=0
	ELSE
		SELECT @MaLoai=Max(right(MaLoai,5)) FROM LoaiTT
		SELECT @MaLoai=
	CASE
	WHEN @MaLoai>=0 and @MaLoai<9 THEN
		'Tin0000'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=9 and @MaLoai<99 THEN
		'Tin000'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=99 and @MaLoai<999 THEN
		'Tin00'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=999 and @MaLoai<9999 THEN
		'Tin0'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=9999 and @MaLoai<99999 THEN
		'Tin'+Convert (char, @MaLoai+1)
	WHEN @MaLoai>=99999 THEN
		Convert (char, @MaLoai+1)
END
RETURN @MaLoai
END

ALTER TABLE LoaiTT
ADD CONSTRAINT defMaLoaiTT DEFAULT dbo.fnGenMaLoaiTT()
FOR MaLoai
---------------------------
 ALTER TABLE DVTinh WITH NOCHECK ADD
CONSTRAINT [PK_MaDVT] PRIMARY KEY CLUSTERED
(
	MaDVT
) ON [PRIMARY]
GO

CREATE FUNCTION fnGenMaDVT()
RETURNS char(8)
AS
BEGIN
	DECLARE @MaDVT char(8)
	IF (SELECT COUNT(MaDVT) FROM DVTinh)=0
		SET @MaDVT=0
	ELSE
		SELECT @MaDVT=Max(right(MaDVT,5)) FROM DVTinh
		SELECT @MaDVT=
	CASE
	WHEN @MaDVT>=0 and @MaDVT<9 THEN
		'DV0000'+Convert (char, @MaDVT+1)
	WHEN @MaDVT>=9 and @MaDVT<99 THEN
		'DV000'+Convert (char, @MaDVT+1)
	WHEN @MaDVT>=99 and @MaDVT<999 THEN
		'DV00'+Convert (char, @MaDVT+1)
	WHEN @MaDVT>=999 and @MaDVT<9999 THEN
		'DV0'+Convert (char, @MaDVT+1)
	WHEN @MaDVT>=9999 and @MaDVT<99999 THEN
		'DV'+Convert (char, @MaDVT+1)
	WHEN @MaDVT>=99999 THEN
		Convert (char, @MaDVT+1)
END
RETURN @MaDVT
END

ALTER TABLE DVTinh
ADD CONSTRAINT defMaMaDVt DEFAULT dbo.fnGenMaDVT()
FOR MaDVT